﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace JEdwonTune {
    public class MySess {
        public class Mapping {
            public static String GetSettingsValue(String rKey) {
                XmlDocument doc = new XmlDocument();
                String filePath = "settings.xml";
                if (!File.Exists(filePath)) {
                    StreamWriter sw = File.CreateText(filePath);
                    sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?><settings></settings>");
                    sw.Close();
                    sw.Dispose();
                    return "";
                }else{
                    doc.Load(filePath);
                    XmlNode ns = doc.SelectSingleNode("//set[@key='" + rKey + "']");
                    return (ns == null) ? "" : ns.Attributes[rKey].Value;
                }
            }
            public static void SetSettingsValue(String rKey,String rValue) {
                XmlDocument doc = new XmlDocument();
                String filePath = "settings.xml";
                if (!File.Exists(filePath)) {
                    StreamWriter sw = File.CreateText(filePath);
                    sw.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?><settings></settings>");
                    sw.Close();
                    sw.Dispose();
                    doc.Load(filePath);
                    XmlElement set = doc.CreateElement("set");
                    XmlAttribute key = doc.CreateAttribute("key");
                    key.Value = rKey;
                    set.Attributes.Append(key);
                    XmlAttribute value = doc.CreateAttribute("value");
                    value.Value = rValue;
                    set.Attributes.Append(value);
                    doc.DocumentElement.AppendChild(set);
                    doc.Save(filePath);
                } else {
                    doc.Load(filePath);
                    XmlNode ns = doc.SelectSingleNode("//set[@key='" + rKey + "']");
                    if (ns == null) {
                        XmlElement set = doc.CreateElement("set");
                        XmlAttribute key = doc.CreateAttribute("key");
                        key.Value = rKey;
                        set.Attributes.Append(key);
                        XmlAttribute value = doc.CreateAttribute("value");
                        value.Value = rValue;
                        set.Attributes.Append(value);
                        doc.DocumentElement.AppendChild(set);
                    } else {
                        ns.Attributes[rKey].Value = rValue;
                    }
                    doc.Save(filePath);
                }
            }
            public static String LastSavePath {
                get {
                    return GetSettingsValue("hibernate_mapping_savepath");
                }
                set {
                    SetSettingsValue("hibernate_mapping_savepath", value);
                }
            }
            public static String Assembly{
                get{
                    return GetSettingsValue("hibernate_mapping_assemble");
                }
                set {
                    SetSettingsValue("hibernate_mapping_assemble", value);
                }
            }
            public static String Namespace {
                get {
                    return GetSettingsValue("hibernate_mapping_namespace");
                }
                set {
                    SetSettingsValue("hibernate_mapping_namespace", value);
                }
            }
        }
    }
}
